package com.dragon.www.haier.net;

import com.dragon.www.haier.net.inter.IClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by Administrator on 2015/10/30 10:36.
 * Author: songxy
 * Email: thinkdata@163.com
 */
public class AppClient implements IClient {

	private String serverUrl;
	public String getServerUrl() {
		return serverUrl;
	}
	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	private String publicParam;
	public String getPublicParam() {
		return publicParam;
	}
	public void setPublicParam(String publicParam) {
		this.publicParam = publicParam;
	}

	protected HttpURLConnection conn = null;

	protected String formatParam() {
		return null;
	}

	/**
	 * 处理返回流
	 * @param input
	 * @return
	 * @throws Exception
	 */
	protected Object doResponse(InputStream input) throws Exception {
		return null;
	}

	protected String formatPublicParam() {
		return String.format(publicParam);
	}

	/**
	 * 暂支持字符串
	 * @param output
	 * @param obj
	 * @throws Exception
	 */
	private void doRequest(OutputStream output, Object obj) throws Exception {
		if (obj instanceof String) {
			String objStr = (String) obj;
			output.write(objStr.getBytes("UTF-8"));
		}
		output.flush();
	}

	private void doRequest(OutputStream output, byte[] input, int offset, int inputLength)
			throws Exception {
		output.write(input, offset, inputLength);
		output.flush();
	}

	private HttpURLConnection openPostConnection(URL url) throws Exception {
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setReadTimeout(10000);
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/octet-stream");
		conn.setRequestProperty("Connection", "Keep-Alive");
		conn.setRequestProperty("Charset", "UTF-8");
		conn.setUseCaches(false);
		return conn;
	}

	private Object getResponse(HttpURLConnection conn) throws Exception {
		int responseCode = conn.getResponseCode();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			return doResponse(conn.getInputStream());
		}else {
			throw new Exception("Http Client Response Code: " + responseCode);
		}
	}

	/**
	 * 提交请求
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public Object doPost(Object input) throws Exception {
		try {
			URL url = new URL(serverUrl + formatPublicParam() + formatParam());
			conn = openPostConnection(url);
			if (input instanceof String) {
				//Content_Length作为InpuStream的长度问题待定？
				conn.setRequestProperty("Content-Length",
						String.valueOf(((String)input).getBytes("UTF-8").length));
			}
			conn.connect();
			doRequest(conn.getOutputStream(), input);
			return getResponse(conn);
		}catch (IOException e) {
			throw new Exception("Http Client Error: "+ e.getMessage());
		}finally {
			if (conn != null)
				conn.disconnect();
		}
	}

	/**
	 * 提交请求
	 * @param input
	 * @param offset
	 * @param inputLength
	 * @return
	 * @throws Exception
	 */
	public Object doPost(byte[] input, int offset, int inputLength) throws Exception {
		URL url = new URL(serverUrl+formatPublicParam()+formatParam());
		HttpURLConnection conn = openPostConnection(url);
		conn.setFixedLengthStreamingMode(inputLength);
		conn.setRequestProperty("Content-Length", String.valueOf(inputLength));
		conn.connect();
		doRequest(conn.getOutputStream(), input, offset, inputLength);
		return getResponse(conn);
	}
}
